﻿using System;
using Neonode.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Reflection;

namespace StopWatch
{
    public class FrmMain : TabView
    {
        private FrmStopWatch frmStopWatch;
        private FrmTimer frmTimer;
        private string text;

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int AddFontResource(string lpName);

        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        public FrmMain()
        {
            InitializeComponent();         
        }

        private void InitializeComponent()
        {
            try
            {
                string fontFile = Assembly.GetExecutingAssembly().GetName().CodeBase;
                fontFile = fontFile.Substring(0, fontFile.LastIndexOf(@"\") + 1) + "subway.ttf";
                AddFontResource(fontFile);
            }
            catch { }
            
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Herbie\StopWatch\");
            if (key == null)
            {
                key = Registry.LocalMachine.CreateSubKey(@"Software\Herbie\StopWatch\");
                key.SetValue("KeepAwake", "0", RegistryValueKind.DWord);
                key.SetValue("LCDColor", "0", RegistryValueKind.DWord);
                key.SetValue("FastReset", "1", RegistryValueKind.DWord);
            }
            else
            {
                key = Registry.LocalMachine.CreateSubKey(@"Software\Herbie\StopWatch\");
                if (key.GetValue("KeepAwake") == null)
                {
                    key.SetValue("KeepAwake", "0", RegistryValueKind.DWord);
                }
                if (key.GetValue("LCDColor") == null)
                {
                    key.SetValue("LCDColor", "0", RegistryValueKind.DWord);
                }
                if (key.GetValue("FastReset") == null)
                {
                    key.SetValue("FastReset", "1", RegistryValueKind.DWord);
                }
            }
            key.Close();

            frmStopWatch = new FrmStopWatch();
            frmTimer = new FrmTimer();
            TabPage tabStopWatch = new TabPage(this.frmStopWatch, "StopWatch", null);
            TabPages.Add(tabStopWatch);
            TabPage tabTimer = new TabPage(this.frmTimer, "Countdown Timer", null);
            TabPages.Add(tabTimer);
        }
    }
}